/**
 * Copyright (C) 2011 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.crm.web.beans;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.enterprise.inject.Produces;
import javax.faces.bean.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Named;

import ar.com.greensoft.neosystem.model.references.EstadoCivil;
import ar.com.greensoft.neosystem.model.references.EstadoInternacion;
import ar.com.greensoft.neosystem.model.references.ISO3166_2AR;
import ar.com.greensoft.neosystem.model.references.RazonSocial;
import ar.com.greensoft.neosystem.model.references.Sexo;
import ar.com.greensoft.neosystem.model.references.TipoDocumento;

/**
 * 
 * @author Jorge E. Villaverde
 * @date 09/01/2012
 * @version 1.0.0
 */
@ApplicationScoped
@Named
public class EnumRefereceBean {
	private static final String BUNBLE_NAME = "MessageResources";

	public SelectItem[] getTiposDocumentos(){
		SelectItem[] items = new SelectItem[TipoDocumento.values().length];
		int i = 0;
		for(TipoDocumento g: TipoDocumento.values()) {
			items[i++] = new SelectItem(g, getMessageFromResourceBundle(g.getLabel()));
		}
		return items;		
	}

	public SelectItem[] getSexos(){
		SelectItem[] items = new SelectItem[Sexo.values().length];
		int i = 0;
		for(Sexo g: Sexo.values()) {
			items[i++] = new SelectItem(g, getMessageFromResourceBundle(g.getLabel()));
		}
		return items;		
	}

	@Produces
	@Named
	public SelectItem[] getEstodosCiviles(){
		SelectItem[] items = new SelectItem[EstadoCivil.values().length];
		int i = 0;
		for(EstadoCivil g: EstadoCivil.values()) {
			items[i++] = new SelectItem(g, getMessageFromResourceBundle(g.getLabel()));
		}
		return items;		
	}
	
	@Produces
	@Named
	public SelectItem[] getCodigosISO3166_2AR(){
		SelectItem[] items = new SelectItem[ISO3166_2AR.values().length];
		int i = 0;
		for(ISO3166_2AR g: ISO3166_2AR.values()) {
			items[i++] = new SelectItem(g, g.getLabel());
		}
		return items;		
	}
	
	@Produces
	@Named
	public SelectItem[] getRazonesSociales(){
		SelectItem[] items = new SelectItem[RazonSocial.values().length];
		int i = 0;
		for(RazonSocial g: RazonSocial.values()) {
			items[i++] = new SelectItem(g, getMessageFromResourceBundle(g.getLabel()));
		}
		return items;		
	}
	
	@Produces
	@Named
	public SelectItem[] getEstadosInternacion(){
		SelectItem[] items = new SelectItem[EstadoInternacion.values().length];
		int i = 0;
		for(EstadoInternacion g: EstadoInternacion.values()) {
			items[i++] = new SelectItem(g, getMessageFromResourceBundle(g.getLabel()));
		}
		return items;		
	}
	
    private static String getMessageFromResourceBundle(String key) {
        String message = "";
    	ResourceBundle bundle = null;
    	String bundlename = BUNBLE_NAME;
    	Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

        try {
    		bundle = ResourceBundle.getBundle(bundlename, locale);
        } catch (MissingResourceException e) {
            // bundle with this name not found;
        }
        if (bundle == null)
            return null;
        try {
            message = bundle.getString(key);
        } catch (Exception e) {
        }
        return message;
    }
    
    public static ClassLoader getCurrentLoader(Object fallbackClass) {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null)
            loader = fallbackClass.getClass().getClassLoader();
        return loader;
    }	
}
